package com.mine.frame.mylandemo.mvp.presenter

import com.mine.frame.common.base.BasePresenter
import com.mine.frame.mylandemo.entity.forick.ControlBean
import com.mine.frame.mylandemo.mvp.iview.IDreamCurtain
import com.third.device.mg.serialport.MGSerialPort
import com.third.device.mg.serialport.utils.toHexString

/**
 * @Date 2022/2/12
 * Description:
 */
class DreamCurtainPresenter(mView: IDreamCurtain) : BasePresenter<IDreamCurtain>(mView) {
    /**
     * 测试ID
     * 大端模式 低位在前，高位在后
     * 高低位均不能为 0x00 和 0xFF
     */
    var  testId = byteArrayOf(0xFE.toByte(), 0xFE.toByte())
    /**
     * 创建控制数据
     */
    fun createControlData(){
        val list = mutableListOf<ControlBean>()
        list.add(ControlBean(true ,"彩虹帘指令" ))

        /*设置设备id*/
        val setId = ControlBean(false ,"彩虹帘-设置ID:0x${testId.toHexString(false)}")
        //设备地址为大端模式，低位在前，高位在后
        setId.command = MGSerialPort.instance.getAutorails().dreamCurtain().setDeviceId(testId)
        list.add(setId)

        val on = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)}打开")
        on.command = MGSerialPort.instance.getAutorails().dreamCurtain().on(testId)
        list.add(on)

        val off = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 关闭")
        off.command = MGSerialPort.instance.getAutorails().dreamCurtain().off(testId)
        list.add(off)

        val stop = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 停止")
        stop.command = MGSerialPort.instance.getAutorails().dreamCurtain().stop(testId)
        list.add(stop)

        val percent = arrayOf(0 , 30 , 50 ,80 ,100)
        percent.forEach {
            val percentControl= ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 运行至${it}%")
            percentControl.command = MGSerialPort.instance.getAutorails().dreamCurtain().runPercent(testId ,it)
            list.add(percentControl)
        }

        val angleList = arrayOf<Short>(0 ,45 , 90 ,135 ,180)
        angleList.forEach {
            val angle = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 设置角度:$it")
            angle.command = MGSerialPort.instance.getAutorails().dreamCurtain().setAngle(testId ,it)
            list.add(angle)
        }


        percent.forEachIndexed { index, item ->
            val perAngle = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 运行至${item}%  角度:${angleList[index]}")
            perAngle.command = MGSerialPort.instance.getAutorails().dreamCurtain().setPercentAndAngle(testId ,item ,angleList[index])
            list.add(perAngle)
        }



        val readPercent = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 读取百分比")
        readPercent.command = MGSerialPort.instance.getAutorails().dreamCurtain().readPercent(testId)
        list.add(readPercent)


        val readDirection = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 读取方向")
        readDirection.command = MGSerialPort.instance.getAutorails().dreamCurtain().readDirection(testId)
        list.add(readDirection)

        val devType = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 读取设备类型")
        devType.command = MGSerialPort.instance.getAutorails().dreamCurtain().readDeviceType(testId)
        list.add(devType)


        val readAll = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 读取当前角度")
        readAll.command = MGSerialPort.instance.getAutorails().dreamCurtain().readAngle(testId)
        list.add(readAll)


        val readAngleC = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 读取当前角度系数")
        readAngleC.command = MGSerialPort.instance.getAutorails().dreamCurtain().readAngleCoefficient(testId)
        list.add(readAngleC)

        val angleListCoefficient = arrayOf<Short>(0 ,50 , 100 ,150 ,200 ,255)
        angleListCoefficient.forEach {
            val angleCoefficient = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 设置角度系数:$it")
            angleCoefficient.command = MGSerialPort.instance.getAutorails().dreamCurtain().setAngleCoefficient(testId ,it)
            list.add(angleCoefficient)
        }




        val zFx = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)}设置默认方向")
        zFx.command = MGSerialPort.instance.getAutorails().dreamCurtain().setDeviceDirection(testId ,true)
        list.add(zFx)

        val fFx = ControlBean(false ,"彩虹帘-ID:0x${testId.toHexString(false)} 设置反方向")
        fFx.command = MGSerialPort.instance.getAutorails().dreamCurtain().setDeviceDirection(testId ,false)
        list.add(fFx)

        mIView.initControlList(list)
    }
}